﻿<Window x:Class="WinDataInspector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:interactions="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:local="clr-namespace:WinDataInspector"
        xmlns:behavior="clr-namespace:WinDataInspector.Behavior"
        xmlns:command="clr-namespace:WinDataInspector.Commands"
        xmlns:component="clr-namespace:WinDataInspector.Components"
        xmlns:converter="clr-namespace:WinDataInspector.Converters"
        xmlns:vm="clr-namespace:WinDataInspector.ViewModel"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:settings="clr-namespace:WinDataInspector.Settings"
        xmlns:validation="clr-namespace:WinDataInspector.Validation"
        WindowStartupLocation="CenterScreen"        
        mc:Ignorable="d"
        Title="{StaticResource AppTitle}"
        AllowDrop="True"
        Icon="Assets/Logo_ico.png"
        Width="{Binding Source={x:Static settings:ViewSettingsLocator.Instance}, Path=Width, Mode=TwoWay}"
        Height="{Binding Source={x:Static settings:ViewSettingsLocator.Instance}, Path=Height, Mode=TwoWay}"        
        >

    <Window.Resources>
        
        <Style TargetType="{x:Type ToolBar}">
            <Setter Property="Background" Value="{StaticResource TitleBrush}" />
        </Style>
        <ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="ListTypes">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="vm:ListTypes" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        
    </Window.Resources>

    <Window.DataContext>
        <vm:MainVM />
    </Window.DataContext>

    <DockPanel LastChildFill="True">
        <ToolBarTray DockPanel.Dock="Top" Background="{StaticResource TitleBrush}">
            <ToolBar Height="32">
                <Button Width="32" Command="{Binding OpenFileCommand}">
                    <Image Source="Assets/1431349863_folder.png" ToolTip="Open file" />
                </Button>
                <Separator />
                <Button Width="32" ToolTip="About" Name="btnAbout" Click="btnAbout_Click">
                    <Image Source="Assets/1434050548_Help.png" ToolTip="Open file" />
                </Button>
            </ToolBar>
        </ToolBarTray>

        <StatusBar DockPanel.Dock="Bottom">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="80*"/>
                            <ColumnDefinition Width="20*"/>
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem>
                <TextBlock Margin="5, 0, 0, 0" Grid.Column="0">
                    <Run Text="Status: "></Run>
                    <Run Text="{Binding Status}"></Run>
                </TextBlock>
            </StatusBarItem>
            <StatusBarItem Grid.Column="1">
                <ProgressBar Grid.Column="1" Height="15" Width="100" IsIndeterminate="{Binding IsProcessing}" />
            </StatusBarItem>
        </StatusBar>


        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="300" Width="70*"></ColumnDefinition>
                <ColumnDefinition Width="5"></ColumnDefinition>
                <ColumnDefinition MinWidth="200" Width="30*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.Resources>
                
            </Grid.Resources>

            <ListBox ItemsSource="{Binding Data}" SelectionMode="Single">
                <!--<ListBox.Resources>
                    <component:DataContextSpy x:Key="Spy" />
                </ListBox.Resources>-->
                <interactivity:Interaction.Behaviors>
                    <behavior:ListBoxJumpBehavior JumpToLineAction="{Binding SelectedIndexData}" />
                </interactivity:Interaction.Behaviors>
                <ListBox.ItemTemplate>
                    <DataTemplate>

                        <VirtualizingStackPanel Orientation="Horizontal">
                            <TextBlock Width="80" Margin="0, 0, 5, 0" Foreground="Blue" Text="{Binding LineNumber, Mode=OneWay, StringFormat={}0x{0:X8}}" />

                            <ItemsControl ItemsSource="{Binding HexData}" Width="350">
                                <ItemsControl.Resources>
                                    <converter:ByteConverter x:Key="ByteConverter" />
                                </ItemsControl.Resources>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBox Width="20" BorderThickness="0" MaxLength="2" FontFamily="Courier New">
                                            <TextBox.Text>
                                                <Binding Path="." StringFormat="X2" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
                                                    <Binding.ValidationRules>
                                                        <validation:HexValidator />
                                                    </Binding.ValidationRules>
                                                </Binding>
                                            </TextBox.Text>
                                            <interactivity:Interaction.Behaviors>
                                                <behavior:HexCellBehavior />
                                            </interactivity:Interaction.Behaviors>
                                        </TextBox>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <ItemsControl ItemsSource="{Binding AsciiData}" Margin="20, 0, 5, 0" Width="300">
                                <ItemsControl.Resources>
                                    <converter:AsciiConverter x:Key="AsciiConverter" />
                                </ItemsControl.Resources>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBox IsReadOnly="True" BorderThickness="0"  Width="13" MaxLength="1" FontFamily="Courier New" MaxLines="1" TextWrapping="NoWrap" Text="{Binding Path=., Mode=OneWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource AsciiConverter}}"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                        </VirtualizingStackPanel>

                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.Style>
                    <Style TargetType="{x:Type ListBox}">
                        <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
                        <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
                        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Data.IsLoading}" Value="True">
                                <Setter Property="ListBox.Cursor" Value="Wait"/>
                                <Setter Property="ListBox.Background" Value="LightGray"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.Style>
            </ListBox>

            <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
                            
            <StackPanel Grid.Column="2">
                <StackPanel.Style>
                    <Style TargetType="{x:Type StackPanel}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Filename}" Value="{x:Null}">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>
                <Expander Header="File information" Margin="5" IsExpanded="True">
                    <Grid Margin="5">
                        <Grid.Resources>
                            <converter:FilenameConverter x:Key="FilenameConverter" />
                            <converter:UnitsizeConverter x:Key="UnitsizeConverter" />
                        </Grid.Resources>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50"></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <TextBlock Margin="0, 5, 0, 0" Text="Name" />
                        <TextBlock Margin="0, 5, 0, 0" Grid.Column="1" Text="{Binding Filename, Converter={StaticResource FilenameConverter}}" />
                        <TextBlock Margin="0, 5, 0, 0" Grid.Row="1" Text="Size" />
                        <TextBlock Margin="0, 5, 0, 0" Grid.Column="1" Grid.Row="1" Text="{Binding FileSize, Converter={StaticResource UnitsizeConverter}}" />
                    </Grid>
                </Expander>
                                
                <!--
                <Expander Header="Search" Margin="5">
                    <StackPanel Margin="5">
                        <TextBlock Margin="0, 0, 0, 0" Text="Search value" />
                        <TextBox Margin="0, 5, 0, 0" Text="{Binding SearchInput}">
                            <TextBox.InputBindings>
                                <KeyBinding Command="{Binding SearchValueCommand}" Key="Return" />
                            </TextBox.InputBindings>
                        </TextBox>
                        <Button Margin="0, 5, 0, 0" Content="Search" Command="{Binding SearchValueCommand}" />
                        <TextBlock Text="Data Type" Margin="0, 5, 0, 0" />
                        <ComboBox Margin="0, 5, 0, 0" ItemsSource="{Binding Source={StaticResource ListTypes}}" SelectedItem="{Binding DataTypeSelected}">

                        </ComboBox>
                    </StackPanel>
                </Expander>
                
                <Expander Header="Navigator" Margin="5">
                    <StackPanel Margin="5">
                        <StackPanel.Resources>
                            <converter:ByteConverter x:Key="ByteConverter" />
                        </StackPanel.Resources>
                        <TextBlock Margin="0, 0, 0, 0" Text="Last address" />
                        <TextBlock Margin="0, 5, 0, 0" Text="{Binding FileSize, StringFormat={}0x{0:X8}}" />
                        <TextBlock Margin="0, 5, 0, 0" Text="Go to address" />
                        <TextBox>
                            <TextBox.Text>
                                <Binding Path="InputAddress" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
                                <Binding.ValidationRules>
                                    <validation:HexValidator />                                    
                                </Binding.ValidationRules>
                            </Binding>
                            </TextBox.Text>
                            <TextBox.InputBindings>
                                <KeyBinding Key="Return" Command="{Binding JumpLineCommand}" />
                            </TextBox.InputBindings>
                        </TextBox>
                        <Button Content="Jump" Margin="0, 5, 0, 0" Command="{Binding JumpLineCommand}" />
                    </StackPanel>
                </Expander>
                -->
                
            </StackPanel>
            
        </Grid>
        
    </DockPanel>

    <interactivity:Interaction.Triggers>
        <interactivity:EventTrigger EventName="ShowMessageEvent" SourceObject="{Binding}">
            <command:InvokeDelegateCommandAction
                Command="{Binding Source={x:Static command:Commands.ShowMessageEventCommand}}"
                CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=InvokeParameter}" />
        </interactivity:EventTrigger>
    </interactivity:Interaction.Triggers>
    
</Window>
